package com.yc.atm;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.net.Socket;
import java.util.Date;
import java.util.Scanner;

/**
 * @author zwei
 * @create 2024-07-19 11:21
 */
public class AtmClient {
    private static Logger logger = Logger.getLogger(AtmClient.class.getName());

    public static void main(String[] args) throws IOException {
        String host = "localhost";
        int port = 12000;
        //创建一个socket
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        try (
                Socket socket = new Socket(host, port);
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream());  //print()可以一行一行形式发送数据
                Scanner sc = new Scanner(socket.getInputStream())) {
            logger.info("连接ATM服务器" + socket.getRemoteSocketAddress() + "成功");
            do {
                System.out.println("**********欢迎来到中国银行ATM**********");
                System.out.println("1. 存");
                System.out.println("2. 取");
                System.out.println("3. 查询");
                System.out.println("4. 退出");
                System.out.println("5. 错误命令(没有命令)");
                System.out.println("=========================================");
                System.out.println("请输入你的选项");
                String command=scanner.nextLine();
                String response=null;
                if ("1".equalsIgnoreCase(command)){
                    printWriter.println("DEPOSIT 1 100");
                }
                else if ("2".equalsIgnoreCase(command)){
                    printWriter.println("WITHDRAW 1 10");
                }
                else if ("3".equalsIgnoreCase(command)){
                    printWriter.println("BALANCE 1 ");
                }
                else if ("5".equalsIgnoreCase(command)){
                    printWriter.println("adfdsf");
                }else {
                    printWriter.println("QUIT");
                    printWriter.flush();
                    flag=false;
                    break;
                }
                printWriter.flush();
                //取服务器的响应
                response=sc.nextLine();
                System.out.println("服务器的响应："+response);
                Gson gson=new Gson();
                //设定 gson处理字符串数据的模板  vue axios
                Type type=new TypeToken<JsonModel<BankAccount>>(){}.getType();
                JsonModel<BankAccount> jm=gson.fromJson(response,type);
                if (jm.getCode()==1){
                    BankAccount ba=jm.getObj();
                    System.out.println(ba.getId()+"\t"+ba.getBalance());
                }else {
                    System.out.println(jm.getErrorMsg());
                }
            }while (flag);
            System.out.println("ATM退出当前账号，程序结束");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
